package main.java.com.silence.service.impl;

import com.silence.constant.Constant;
import com.silence.dao.IBillDao;
import com.silence.pojo.Bill;
import com.silence.service.IBillService;
import com.silence.vo.Pager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: silence
 * Date: 17-6-3
 */
@Service("billService")
public class BillServiceImpl implements IBillService {

    @Resource
    private IBillDao billDao = null;

    @Override
    public Bill findById(Integer id) {
        return billDao.selectByPrimaryKey(id);
    }

    @Override
    public Pager<Bill>  findAllByOwnerId(Integer ownerId, Integer pageIndex) {

        int count = billDao.countByOwner(ownerId);  // 总记录数
        // 总页数
        int totalPage = count / Constant.PAGE_SIZE % Constant.PAGE_SIZE == 0 ?
                count / Constant.PAGE_SIZE : count / Constant.PAGE_SIZE + 1;

        // 构造查询需要的参数
        Map<String, Object> option = new HashMap<String, Object>();
        option.put("pageSize", Constant.PAGE_SIZE);
        option.put("pageOffset", (pageIndex - 1) * Constant.PAGE_SIZE);

        List<Bill> bills = billDao.selectAllByOwnerId(ownerId, option);


        Pager<Bill> pager = new Pager<Bill>();

        pager.setTotalPage(totalPage);
        pager.setPageIndex(pageIndex);  // 当前页面
        pager.setPageSize(Constant.PAGE_SIZE);  // 每一页的个数
        pager.setPageOffset((pageIndex - 1) * Constant.PAGE_SIZE); // 当前页面第一个的序列
        pager.setData(bills);
        pager.setTotalRecord(count);
        return pager;
    }

    @Override
    public Pager<Bill> findAllByOwnerEmailAndCreateDate(String email, Integer pageIndex, Date createDate){

        // 初始化一些数据
        int count = billDao.countByEmailAndCreateDate(email, createDate);
        int totalPage = count % Constant.PAGE_SIZE == 0 ?
                count / Constant.PAGE_SIZE : count / Constant.PAGE_SIZE + 1;

        // 构造查询参数
        Map<String, Object> params = new HashMap<String, Object>();

//        params.put("email", email);
        params.put("pageIndex", pageIndex);
        params.put("createDate", createDate);
        params.put("pageSize", Constant.PAGE_SIZE);
        params.put("pageOffset", (pageIndex - 1) * Constant.PAGE_SIZE);

        List<Bill> bills = billDao.selectAllByOwnerEmailAndCreateDate(email, params);

        // 构造返回值
        Pager<Bill> billPager = new Pager<Bill>();
        billPager.setPageIndex(pageIndex);
        billPager.setTotalPage(totalPage);
        billPager.setData(bills);
        billPager.setTotalRecord(count);
        return billPager;
    }

    @Override
    public Pager<Bill> findAllByOwnerEmail(String email, Integer pageIndex) {

        // 初始化一些数据
        int count = billDao.countByEmail(email);
        int totalPage = count % Constant.PAGE_SIZE == 0 ?
                count / Constant.PAGE_SIZE : count / Constant.PAGE_SIZE + 1;

        // 构造查询参数
        Map<String, Object> option = new HashMap<String, Object>();
        option.put("pageSize", Constant.PAGE_SIZE);
        option.put("pageOffset", Constant.PAGE_SIZE * (pageIndex - 1));


        List<Bill> bills = billDao.selectAllByOwnerEmail(email, option);

        Pager<Bill> pager = new Pager<Bill>();
        pager.setPageIndex(pageIndex);  // 当前页面
        pager.setPageSize(Constant.PAGE_SIZE);  // 每一页的个数
        pager.setPageOffset((pageIndex - 1) * Constant.PAGE_SIZE); // 当前页面第一个的序列
        pager.setTotalPage(totalPage);
        pager.setTotalRecord(count);
        pager.setData(bills);

        return pager;
    }

    @Override
    public int update(Bill bill) {
        return billDao.update(bill);
    }

    @Override
    public int deleteById(Integer id) {
        return billDao.deleteByPrimaryKey(id);
    }

    @Override
    public int add(Bill bill) {
        return billDao.insert(bill);
    }

//    public Pager<Bill> findAllByOwnerId(Integer ownerId){
//        Map<String, Object> paras = new HashMap();
//
//        int pageSize = /*SystemContext.getPageSize*/;
//        int pageOffset = /*SystemContext.getPageOffset*/;
//
//        paras.put()
//    }
}
